import Vue from 'vue'
import vueRouter from "vue-router";
import musicHall from '@/views/musicHall/musicHall.vue'
import myCollect from '@/views/myCollcect/myCollect.vue'
import likeMusic from '@/views/likeMusic/likeMusic.vue'
import recentlyPlay from '@/views/recentlyPlay/recentlyPlay.vue'
import musicList from '@/views/musicList.vue'
import searchV from '@/views/search/searchV.vue'
import singerHpage from '@/views/singer/singerHpage.vue'
import userPage from '@/views/userPage/userPage.vue'
import albumPage from '@/views/albumPage/albumPage.vue'
import login from '@/views/login-register/login.vue'
import register from '@/views/login-register/register.vue'

Vue.use(vueRouter) //全局使用router
const routes = [
  {
    path:"/musicHall",
    alias:"/",
    name:"musicHall",
    component:()=>import('@/views/musicHall/musicHall.vue')
  },
  {
    path:"/myCollect",
    name:"myCollect",
    component:()=>import('@/views/myCollcect/myCollect.vue')
  },
  {
    path:"/likeMusic",
    name:"likeMusic",
    component:()=>import('@/views/likeMusic/likeMusic.vue')
  },
  {
    path:"/recentlyPlay",
    name:"recentlyPlay",
    component:()=>import('@/views/recentlyPlay/recentlyPlay.vue')
  },
  {
    path:"/musicList",
    name:"musicList",
    component:()=>import('@/views/musicList.vue')
  },
  {
    path:"/search",
    name:"search",
    component:()=>import('@/views/search/searchV.vue')
  },
  {
    path:"/singerHpage",
    name:"singerHpage",
    component:()=>import('@/views/singer/singerHpage.vue')
  },
  {
    path:"/userPage",
    name:"userPage",
    component:()=>import('@/views/userPage/userPage.vue')
  },
  {
    path:"/albumPage",
    name:"albumPage",
    component:()=>import('@/views/albumPage/albumPage.vue')
  },
  {
    path:"/login",
    name:"login",
    component:()=>import('@/views/login-register/login.vue')
  },
  {
    path:"/register",
    name:"register",
    component:()=>import('@/views/login-register/register.vue')
  }

]
const router = new vueRouter({
  routes,
  mode: "history"
})

// //解决bug
// const originalPush = router.prototype.push
// router.prototype.push = function push(location, onResolve, onReject) {
//     if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//     return originalPush.call(this, location).catch(err => err)
// }
router.beforeEach((to, from, next) => {
  const name = localStorage.getItem('qtUser');//查看本地存储上是否有name对象
  if (name || to.path === '/musicHall' || to.path === "/musicList") {//短路逻辑，有就可以继续执行，没有就跳转到登录页面
    next()
  } else {
    next('/musicHall')//跳转登录页面
  }
})
export default router

